package com.yzf.demo.demo.Thread.lock;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * Created by 于占峰 on 2020/4/4/004.
 */
public class ConditionDemoWait extends Thread {
    //使用concurrent中的锁
    private Lock lock;

    private Condition condition;

    public ConditionDemoWait(Lock lock, Condition condition) {
        this.lock = lock;
        this.condition = condition;
    }

    @Override
    public void run() {
        System.out.println("begin - Condition--wait");
        try {
            lock.lock();//加锁
            condition.await();//必须在锁的范围内 阻塞当前线程
            System.out.println("end - Condition--wait");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//释放锁
        }
    }
}
